package com.service.rd;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.sql.ast.statement.SQLIfStatement.Else;
import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.common.model.Global;
import com.dao.rd.RdPlanDao;
import com.dao.rd.RdPlanMmMapper;
import com.model.rd.PlanMm;
import com.model.rd.PlanModel;
import com.model.rd.PlanVoModel;
import com.service.purchase.CommonService;

@Service("rdPlanService")
public class PlanService {

	static final Logger logger = LoggerFactory.getLogger(PlanService.class);

	@Autowired
	RdPlanMmMapper planMmMapper;
	@Autowired
	RdPlanDao planDao;
	@Autowired
	CommonService commonService;

	/**
	 * 计划明细列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectPlanMmList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		List<PlanMm> list = planMmMapper.selectList(queryMap);
		int count = planMmMapper.selectListCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("selectPlanMmList:{}", list);

		return httpResult;
	}

	/**
	 * 计划列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectPlanList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		List<PlanModel> list = planDao.selectList(queryMap);
		int count = planDao.selectListCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("selectPlanList:{}", list);

		return httpResult;
	}

	/**
	 * 保存采购计划
	 * 
	 * @param purchasePlanMm
	 * @return
	 */
	@Transactional
	public HttpResult savePlan(PlanVoModel purchasePlanVo) {
		if (purchasePlanVo == null || purchasePlanVo.getPurchasePlanMms() == null) {
			return HttpResult.error("参数不能为空");
		}

		int result = 0;
		String idString = purchasePlanVo.getPlanId();
		if (!StringUtils.isNotNull(idString)) {
			idString = UUID.randomUUID().toString();
			purchasePlanVo.setPlanId(idString);
			String codeString = commonService.createCode("rd_plan", "plan_code");
			purchasePlanVo.setPlanCode(codeString);
			result = planDao.insert(purchasePlanVo);
		} else {
			result = planDao.update(purchasePlanVo);
		}
//		planMmMapper.delete(idString);
		for (PlanMm planMm : purchasePlanVo.getPurchasePlanMms()) {
			planMm.setPlanId(idString);
			planMm.setPlanCode(purchasePlanVo.getPlanCode());
			planMm.setPlanName(purchasePlanVo.getPlanName());
			planMm.setStatus(purchasePlanVo.getStatus());
			if (planMmMapper.update(planMm) == 0) {
				planMm.setPlanStockNum(planMm.getPlanPurNum());
				planMmMapper.insert(planMm);
			}
		}
		HttpResult httpResult = new HttpResult();

		if (result > 0) {
			httpResult.setData(purchasePlanVo);
			httpResult.setMsg(purchasePlanVo.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
			return httpResult;
		} else {
			return HttpResult.error(purchasePlanVo.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
		}
	}

	/**
	 * 回退
	 * 
	 * @param purchasePlanMm
	 * @return
	 */
	@Transactional
	public HttpResult backPlan(String planId) {
		if (!StringUtils.isNotNull(planId)) {
			return HttpResult.error("参数不能为空!");
		}
		PlanModel planModel = planDao.load(planId);
		planModel.setStatus(Global.NO + "");
		int result = planDao.update(planModel);
		// 明细表改为非提交状态
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("planId", planId);
		List<PlanMm> planMms = planMmMapper.selectList(hashMap);
		for (PlanMm planMm : planMms) {
			planMm.setStatus(Global.NO + "");
			result = planMmMapper.update(planMm);
		}
		if (result > 0) {
			return HttpResult.ok("回退成功");
		} else {
			return HttpResult.error("不存在此采购计划");
		}
	}

	/**
	 * 删除
	 *
	 * @param
	 * @return
	 */
	public HttpResult delete(String planId) {
		HttpResult httpResult = new HttpResult();
		if (!StringUtils.isNotNull(planId)) {
			return HttpResult.error("参数不能为空");
		}
		int result = planMmMapper.delete(planId);
		planDao.delete(planId);
		if (result > 0) {
			httpResult.setMsg("删除成功！");
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("不存在此条数据！");
		}
		return httpResult;
	}

	/**
	 * 导出
	 * 
	 * @param queryMap
	 * @param response
	 */
	public void exportPlan(Map<String, Object> queryMap, HttpServletResponse response) {
		List<PlanMm> list = planMmMapper.selectList(queryMap);

		logger.info("exportPlan:{}", list);
		try {
			EasyExcel.write(response.getOutputStream(), PlanMm.class).sheet("模板").doWrite(list);
		} catch (IOException e) {
			logger.error("导出错误:{}", e.getMessage());
		}

	}
}
